import 'dart:async';

import 'package:flutter/material.dart';

class lajifenlei extends StatefulWidget {
  const lajifenlei({super.key});

  @override
  State<lajifenlei> createState() => _lajifenleiState();
}

class _lajifenleiState extends State<lajifenlei>
    with SingleTickerProviderStateMixin {
  late final PageController controller;
  int selectCount = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    controller = PageController();
    Timer.periodic(Duration(seconds: 3), (timer) {
      setState(() {
        selectCount++;
      });
      print(selectCount);
      if (selectCount > 2) {
        controller.animateToPage(selectCount,
            duration: Duration(milliseconds: 500), curve: Curves.ease);
        selectCount = 0;
        Future.delayed(Duration(milliseconds: 500), () {
          controller.jumpToPage(selectCount);
        });
      } else {
        controller.animateToPage(selectCount,
            duration: Duration(milliseconds: 500), curve: Curves.ease);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        children: [
          SizedBox(
              height: 200,
              child: Stack(
                children: [
                  PageView(
                    controller: controller,
                    onPageChanged: (value) {},
                    children: [
                      Container(
                        height: 160,
                        color: Colors.pink[200],
                        child: Center(child: Text('1')),
                      ),
                      Container(
                        height: 160,
                        color: Colors.yellow,
                        child: Center(child: Text('2')),
                      ),
                      Container(
                        height: 160,
                        color: Colors.blue,
                        child: Center(child: Text('3')),
                      ),
                      Container(
                        height: 160,
                        color: Colors.pink[200],
                        child: Center(child: Text('1')),
                      ),
                    ],
                  ),
                  Row(children: List.generate(3, (index) => newMethod(index)))
                ],
              )),
        ],
      ),
    );
  }

  Widget newMethod(int count) {
    return Container(
      height: 20,
      width: 20,
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        color: count == selectCount ? Colors.red : Colors.grey,
      ),
    );
  }
}
